package com.coffeeloversupportlibrary.coffeelover.domain.article;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.coffeelover.domain.article.adapter.MainAdapter;
import com.coffeeloversupportlibrary.core.base.BaseFragment;
import com.coffeeloversupportlibrary.databinding.ArticleFragmentBinding;

/**
 * @author puyv
 * @createTime 2021/11/6 20:08
 * @projectName CoffeeLoverSupportLibrary
 * @className ArticleFragment.java
 * @description TODO
 */
public class ArticleFragment extends BaseFragment<ArticleViewModel, ArticleFragmentBinding> {


    // UIChangeCallback 即：ViewModel LiveData 改变时（setValue）回调观察者实现类方法
    @Override
    public void registerUIChangeCallBack() {
        getViewModel().getLoadData().observe(this, new Observer<MainAdapter>() {
            @Override
            public void onChanged(MainAdapter mainAdapter) {
                if(mainAdapter!=null){
                    mainAdapter.repeatBuild(getChildFragmentManager(),getBinding().articleParentLayout);
                    getBinding().recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
                    getBinding().recyclerView.setAdapter(mainAdapter);
                }
            }
        });

    }

    @Override
    public int getLayoutId() {
        return R.layout.article_fragment;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        getViewModel().init();
    }
}

